package com.bocai.kelalacar.ui.car.set.presenter;

import android.util.Log;

import com.bocai.kelalacar.base.BaseResponse;
import com.bocai.kelalacar.bean.ApplyWayBean;
import com.bocai.kelalacar.net.Api;
import com.bocai.kelalacar.ui.car.set.contract.WithdrawTypeContract;
import com.bocai.kelalacar.util.AESUtils;
import com.bocai.kelalacar.util.RetryWithDelay;
import com.google.gson.Gson;

import javax.inject.Inject;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yuanfei on 2017/7/3.
 */

public class WithdrawTypePresenter extends WithdrawTypeContract.presenter {

    private final WithdrawTypeContract.view view;
    private final Api api;
    public String start = "{\"content\":";
    public String end = "}";

    @Inject
    public WithdrawTypePresenter(WithdrawTypeContract.view view, Api api) {
        this.view = view;
        this.api = api;
    }

    @Override
    public void applyWay() {
        view.showLoading("");
        api.applyWay().subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                if (baseResponse.getReturnCode().equals("200")){
                    String result = AESUtils.decode(baseResponse.getData());
                    Log.e("result",result);
                    ApplyWayBean bean = new Gson().fromJson(result,ApplyWayBean.class);
                    view.applyWaySuccess(bean);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void addApplyWay(String OpenBank, String PaymentType, String RealName, String Account) {
        view.showLoading("");
        api.addApplyWay(OpenBank,PaymentType,RealName,Account).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                if (baseResponse.getReturnCode().equals("200")){
                    view.addApplyWaySuccess();
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void deleteApplyWay(final int index, String wayId) {
        api.deleteApplyWay(wayId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                if (baseResponse.getReturnCode().equals("200")){
                    view.deleteApplyWaySuccess(index);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void walletApply(String WayId, String Money, boolean IsBail) {
        api.walletApply(WayId,Money,IsBail).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                if (baseResponse.getReturnCode().equals("200")){
                    view.walletApplySuccess();
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }
}
